package org.gnyxy.controller.background;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.gnyxy.constant.DeleteConstant;
import org.gnyxy.constant.StatusConstant;
import org.gnyxy.dto.PageQueryDTO;
import org.gnyxy.entity.Type;
import org.gnyxy.result.PageResult;
import org.gnyxy.result.Result;
import org.gnyxy.service.TypeService;
import org.gnyxy.vo.MaterialVO;
import org.gnyxy.vo.TypeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("AdminTypeController")
@RequestMapping("/background/type")
@Api(tags = "商家后台-菜品分类")
@Slf4j
public class TypeController {
    @Autowired
    private TypeService typeService;

    /**
     * 添加菜品
     * @param type
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加菜品分类（上传图片调用通用的上传接口）")
    @CacheEvict(cacheNames = "typeCache",allEntries = true)
    public Result add(@RequestBody Type type){
        log.info("新增的分类为: {}",type);
        typeService.add(type);
        return Result.success();
    }

    /**
     * 分页查询
     * @param pageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(PageQueryDTO pageQueryDTO){
        log.info("分页查询: {}",pageQueryDTO);
        PageResult result = typeService.page(pageQueryDTO);
        return Result.success(result);
    }

    /**
     * 根据id查询菜品分类
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品分类")
    public Result<TypeVO> getById(@PathVariable Long id){
        log.info("查询菜品分类的id: {}",id);
        TypeVO typeVO = typeService.getById(id);
        return Result.success(typeVO);
    }

    /**
     * 更新菜品分类
     * @param type
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("更新菜品分类")
    @CacheEvict(cacheNames = "typeCache",allEntries = true)
    public Result update(@RequestBody Type type){
        log.info("更新的菜品分类为: {}",type);
        typeService.update(type);
        return Result.success();
    }

    /**
     * 删除菜品分类
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    @ApiOperation("删除菜品分类")
    @CacheEvict(cacheNames = "typeCache",allEntries = true)
    public Result deleteById(@PathVariable Long id){
        log.info("删除的菜品分类id为: {}",id);
        typeService.deleteById(id);
        return Result.success();
    }

    /**
     * 设置菜品状态
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("设置菜品分类状态")
    @CacheEvict(cacheNames = "typeCache",allEntries = true)
    public Result setStatus(@PathVariable Integer status,Long id){
        log.info("设置菜品分类id: {},状态为: {}",id,status.equals(StatusConstant.ENABLE) ? "启用":"禁用");
        typeService.setStatus(status,id);
        return Result.success();
    }

    /**
     * 查看未被禁用的菜品材料
     * @return
     */
    @GetMapping("/getTrueStatus")
    @ApiOperation("查看未被禁用的分类")
    public Result<List<TypeVO>> getTrueStatus(String name){
        log.info("查看未被禁用的分类: {}",name);
        List<TypeVO> typeVOS = typeService.getTrueStatus(name);
        return Result.success(typeVOS);
    }
}
